package com.sunfeng.chap2;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 验证write方法时从通道当前位置开始的 最终文件内容为： abcoooooo1oooooo2
 */
public class Test3_1 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:\\a.txt"));

        FileChannel fileChannel = fileOutputStream.getChannel();

        fileChannel.write(ByteBuffer.wrap("abcdefgh".getBytes()));

        fileChannel.position(3);

        ByteBuffer byteBuffer = ByteBuffer.wrap("oooooo1".getBytes());
        ByteBuffer byteBuffer1 = ByteBuffer.wrap("oooooo2".getBytes());

        ByteBuffer[] byteBuffers = {byteBuffer, byteBuffer1};

        fileChannel.write(byteBuffers);

        fileChannel.close();

        fileOutputStream.close();
    }
}
